{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e84fd778",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 <class 'int'>\n",
      "1.3333333333333333 <class 'float'>\n",
      "1 <class 'int'>\n",
      "7.0 <class 'float'>\n",
      "(4+2j) <class 'float'> <class 'float'>\n",
      "(0.3333333333333333+0.6666666666666666j) <class 'complex'>\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#例1-19\n",
    "a=4#整型\n",
    "b=3#整型\n",
    "c=a+b#整型\n",
    "print(c,type(c))\n",
    "c=a/b#浮点型\n",
    "print(c,type(c))\n",
    "c=a//b#整型\n",
    "print(c,type(c))\n",
    "b=3.0#浮点型\n",
    "c=a+b#浮点型\n",
    "print(c,type(c))\n",
    "a=1+2j#复数型\n",
    "b=3#整型\n",
    "c=a+b\n",
    "print(c,type(c.real),type(c.imag))\n",
    "c=a/b#实部、虚部为浮点型的复数\n",
    "print(c,type(c))\n",
    "a=0.1+0.2\n",
    "b=0.3\n",
    "print(a==b)\n",
    "print(abs(a-b)<1e-10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2023a18c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4/3\n",
      "6004799503160661/18014398509481984\n",
      "1/3\n"
     ]
    }
   ],
   "source": [
    "#例1-20\n",
    "from fractions import Fraction as F\n",
    "a=F(4,1)\n",
    "b=F(3)\n",
    "c=a/b\n",
    "print(c)\n",
    "c=F(1/3)\n",
    "print(c)\n",
    "print(c.limit_denominator())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "74f09dff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0+1/2∙x+0∙x**2+1/3∙x**3\n",
      "0.0+0.5∙x+0.0∙x**2+0.3333333333333333∙x**3\n",
      "1-2∙x+1∙x**2\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "#例1-21\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "def exp(a):\n",
    "    n=len(a)\n",
    "    s=''\n",
    "    if n==0:#零多项式\n",
    "        s=s+'0'\n",
    "    else:#非零多项式\n",
    "        for i in range(n):\n",
    "            if i==0:#常数项\n",
    "                s=s+'%s'%a[i]\n",
    "            if i==1 and a[i]>=0:#非负1次项\n",
    "                s=s+'+%s∙x'%a[i]\n",
    "            if i==1 and a[i]<0:#负1次项\n",
    "                s=s+'%s∙x'%a[i]\n",
    "            if i>1 and a[i]>=0:#非负项\n",
    "                s=s+'+%s∙x**%d'%(a[i],i)\n",
    "            if i>1 and a[i]<0:#负项\n",
    "                s=s+'%s∙x**%d'%(a[i],i)\n",
    "    return s\n",
    "a=[F(0),F(1,2),F(0),F(1,3)]\n",
    "b=[0.0,0.5,0.0,1/3]\n",
    "c=np.array([1,-2,1])\n",
    "d=[0]\n",
    "print(exp(a))\n",
    "print(exp(b))\n",
    "print(exp(c))\n",
    "print(exp(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec174615",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=10001\n",
      "b=10101\n",
      "a<<2=1000100, 68\n",
      "b>>2=101, 5\n",
      "a|b=10101\n",
      "a&b=10001\n",
      "a^b=100\n",
      "~a=-10010\n",
      "a逐位取反=1110\n"
     ]
    }
   ],
   "source": [
    "#例1-22\n",
    "a=17\n",
    "b=21\n",
    "print('a=%s'%\"{0:b}\".format(a))#a的2进制表达式\n",
    "print('b=%s'%\"{0:b}\".format(b))#b的2进制表达式\n",
    "print('a<<2=%s, %d'%(\"{0:b}\".format(a<<2),a<<2))#a左移2位\n",
    "print('b>>2=%s, %d'%(\"{0:b}\".format(b>>2),b>>2))#b右移2位\n",
    "print('a|b=%s'%\"{0:b}\".format(a|b))#a与b按位或\n",
    "print('a&b=%s'%\"{0:b}\".format(a&b))#a与b按位与\n",
    "print('a^b=%s'%\"{0:b}\".format(a^b))#a与b按位异或\n",
    "print('~a=%s'%\"{0:b}\".format(~a))#a的带符号位补码按位反\n",
    "n=a.bit_length()#a的2进制位数\n",
    "b=2**n-1#n位1\n",
    "print('a逐位取反=%s'%\"{0:b}\".format(a^b))#a的原码按位反"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0139ec7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('A', 119, 8523904)\n",
      "('B', 34834, 4229)\n",
      "('C', 12976547, 20)\n"
     ]
    }
   ],
   "source": [
    "#例1-23\n",
    "def ipAnaly(a):\n",
    "    b32=2**32-1#32位1\n",
    "    m=255<<24#第1个字节全为1其他为0\n",
    "    t=(a&m)>>24#地址的第1个字节\n",
    "    if 1<=t<=126:#A类地址\n",
    "        t1='A'#地址类型\n",
    "        n=t#网络号\n",
    "        m1=m^b32#掩码255.0.0.0的反码\n",
    "        p=a&m1#主机号\n",
    "    if 128<=t<=191:#B类地址\n",
    "        t1='B'#地址类型\n",
    "        m=(2**16-1)<<16#掩码255.255.0.0\n",
    "        n=(a&m)>>16#网络号\n",
    "        m1=m^b32#掩码的反码\n",
    "        p=a&m1#主机号\n",
    "    if 192<=t<=223:#C类地址\n",
    "        t1='C'#地质类型\n",
    "        m=(2**24-1)<<8#掩码255.255.255.0\n",
    "        n=(a&m)>>8#网络号\n",
    "        m1=m^b32#掩码的反码\n",
    "        p=a&m1#主机号\n",
    "    return t1, n, p\n",
    "a=2005012608#地址119.130.16.128\n",
    "print(ipAnaly(a))\n",
    "a=2282885253#地址136.18.16.133\n",
    "print(ipAnaly(a))\n",
    "a=3321996052#地址198.1.163.20\n",
    "print(ipAnaly(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5c3f729a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1-2∙x+1∙x**2\n",
      "-1/2∙x+1/3∙x**3\n",
      "-0.5∙x+0.3333333333333333∙x**3\n",
      "False\n",
      "True\n",
      "1-5/2∙x+1∙x**2+1/3∙x**3\n",
      "-0.75∙x+0.5∙x**3\n",
      "0\n",
      "9+12∙x+10∙x**2+4∙x**3+1∙x**4\n"
     ]
    }
   ],
   "source": [
    "#例1-24、1-25、练习1-7\n",
    "import numpy as np\n",
    "from fractions import Fraction as F\n",
    "def exp(a):\n",
    "    n=len(a)\n",
    "    s=''\n",
    "    start=0\n",
    "    while start<n and a[start]==0:#查找第一个非零项下标start\n",
    "        start+=1\n",
    "    if start>=n:#零多项式\n",
    "        s+='0'\n",
    "    else:#非零多项式\n",
    "        if start==0:#非零常数项\n",
    "            s+='%s'%a[start]\n",
    "        if start==1:#非零1次项\n",
    "            s+='%s∙x'%a[start]\n",
    "        if start>1:#首项次数大于1\n",
    "            s+='%s∙x^%d'%(a[start],start)\n",
    "        for i in range(start+1,n):#首项后各项\n",
    "            if i==1:#1次项\n",
    "                if a[i]>0:#正1次项\n",
    "                    s=s+'+%s∙x'%a[i]\n",
    "                if a[i]<0:#负1次项\n",
    "                    s=s+'%s∙x'%a[i]\n",
    "            else:#非1次项\n",
    "                if a[i]>0:#正项\n",
    "                    s=s+'+%s∙x**%d'%(a[i],i)\n",
    "                if a[i]<0:#负项\n",
    "                    s=s+'%s∙x**%d'%(a[i],i)\n",
    "    return s\n",
    "class myPoly:#多项式类\n",
    "    def __init__(self, coef):#初始化\n",
    "        c=np.trim_zeros(coef,trim='b')#删除高次零系数\n",
    "        self.coef=np.array(c)#设置多项式系数\n",
    "        self.degree=(self.coef).size-1#设置多项式次数\n",
    "    def __eq__(self, other):#相等\n",
    "        if self.degree!=other.degree:#次数不等\n",
    "            return False\n",
    "        return (abs(self.coef-other.coef)<1e-10).all()\n",
    "        #return (self.coef==other.coef).all()\n",
    "    def __str__(self):#输出表达式\n",
    "        return exp(self.coef)        \n",
    "    def __add__(self, other):#运算符“+”\n",
    "        n=max(self.degree,other.degree)+1#系数个数\n",
    "        a=np.append(self.coef,[0]*(n-self.coef.size))#补长\n",
    "        b=np.append(other.coef,[0]*(n-other.coef.size))#补长\n",
    "        return myPoly(a+b)#创建并返回多项式和\n",
    "    def __rmul__(self, k):#右乘数k\n",
    "        c=self.coef*k#各系数与k的积\n",
    "        return myPoly(c)\n",
    "    def __neg__(self):#负多项式\n",
    "        return (-1)*self\n",
    "    def __sub__(self, other):#运算符“-”\n",
    "        return self+(-other)\n",
    "    def __mul__(self,other):#运算符“*”\n",
    "        m=self.degree\n",
    "        n=other.degree\n",
    "        a=np.append(self.coef,[0]*n)\n",
    "        b=np.append(other.coef,[0]*m)\n",
    "        c=[]\n",
    "        for s in range(1, m+n+2):\n",
    "            cs=(a[:s]*np.flip(b[:s])).sum()\n",
    "            c.append(cs)\n",
    "        return myPoly(c)\n",
    "    def val(self,x):#多项式在x处的值\n",
    "        n=self.degree#次数\n",
    "        power=np.array([x**k for k in range(n+1)])#x各次幂\n",
    "        v=((self.coef)*power).sum()#多项式的值\n",
    "p=myPoly(np.array([1,-2,1]))\n",
    "q=myPoly([F(0),F(-1,2),F(0),F(1,3)])\n",
    "r=myPoly([0.0,-0.5,0.0,1/3])\n",
    "k=1.5\n",
    "print(p)\n",
    "print(q)\n",
    "print(r)\n",
    "print(p==q)\n",
    "print(q==r)\n",
    "print(p+q)\n",
    "print(k*q)\n",
    "print(q-r)\n",
    "f=myPoly([3, 2, 1])\n",
    "g=myPoly([3, 2, 1])\n",
    "print(f*g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "807054db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
